package com.jjh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jjh.common.controller.BaseController;
import com.jjh.common.pojo.InfoEnum;
import com.jjh.common.util.ConstatFinalUtil;
import com.jjh.common.util.PageInfoUtil;
import com.jjh.users.pojo.ACourse;
import com.jjh.users.pojo.AStudent;
import com.jjh.users.pojo.AStudentFav;
import com.jjh.users.pojo.ATeacher;
import com.jjh.users.service.IUsersService;

/**
 * 后台controller
 * @author MyJJh
 *
 */
@Controller
@RequestMapping("/back/teachers")
public class TeacherBackController extends BaseController
{
	/**
	 * service
	 */
	@Autowired
	private IUsersService usersService;
	
	
	/**
	 * 登录后主页
	 * @return
	 */
	@RequestMapping("/mainTeacher")
	public String mainTeacher()
	{
		ConstatFinalUtil.LOGGER.info("=======mainTeacher======");
		return null ;
	}
	
	/**
	 * 欢迎界面
	 * @return
	 */
	@RequestMapping("/welcome")
	public String welcome()
	{
		ConstatFinalUtil.LOGGER.info("=======welcome======");
		return null ;
	}
	
	/**
	 * 老师列表
	 * @return
	 */
	@RequestMapping("/teachersList")
	public String teachersList(HttpServletRequest request , Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====teachersList=======");
		
		/* 获取分页工具 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		
		/* 获取搜索的条件 */
		Map<String,Object> condMap = this.proccedSearch(request);
		
		/* 调用service的方法 */
		List<ATeacher> list = this.usersService.findCondListTeacherService(pageInfoUtil, condMap);
		
		/* 将条件存储到四大作用中 */
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		model.addAttribute("list", list);
		
		return null ;
	}
	
	
	/**
	 * 课程列表
	 * @return
	 */
	@RequestMapping("/coursesList")
	public String coursesList(HttpServletRequest request , Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====coursesList=======");
		
		/* 获取分页工具 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		
		/* 获取搜索的条件 */
		Map<String,Object> condMap = this.proccedSearch(request);
		
		/* 调用service的方法 */
		List<ACourse> list = this.usersService.findCondListCourseService(pageInfoUtil, condMap);
		
		/* 将条件存储到四大作用中 */
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		model.addAttribute("list", list);
		
		return null ;
	}
	
	/**
	 * 学生列表
	 * @return
	 */
	@RequestMapping("/studentsList")
	public String studentsList(HttpServletRequest request , Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====studentsList=======");
		
		/* 获取分页工具 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		
		/* 获取搜索的条件 */
		Map<String,Object> condMap = this.proccedSearch(request);
		
		/* 处理关键字 */
		String scoreSt = request.getParameter("scoreSt");
		if(scoreSt == null)
		{
			scoreSt = "" ;
		}
		scoreSt = scoreSt.trim();
		condMap.put("scoreSt", scoreSt);
		/* 存储大四大作用域中 */
		request.setAttribute("scoreSt", scoreSt);
		String scoreEd = request.getParameter("scoreEd");
		if(scoreEd == null)
		{
			scoreEd = "" ;
		}
		scoreEd = scoreEd.trim();
		condMap.put("scoreEd", scoreEd);
		
		/* 存储大四大作用域中 */
		request.setAttribute("scoreEd", scoreEd);
		
		/* 调用service的方法 */
		List<AStudent> list = this.usersService.findCondListStudentService(pageInfoUtil, condMap);
		
		/* 将条件存储到四大作用中 */
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		model.addAttribute("list", list);
		
		return null ;
	}
	
	/**
	 * 学生收藏列表
	 * @return
	 */
	@RequestMapping("/studentsFavList")
	public String studentsFavList(HttpServletRequest request , Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====studentsList=======");
		
		/* 获取分页工具 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		
		/* 获取搜索的条件 */
		Map<String,Object> condMap = this.proccedSearch(request);
		
		/* 调用service的方法 */
		List<AStudentFav> list = this.usersService.findCondListStudentFavService(pageInfoUtil, condMap);
		
		/* 将条件存储到四大作用中 */
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		model.addAttribute("list", list);
		
		return null ;
	}
	
	/**
	 * 打开更新页面
	 * @return
	 */
	@RequestMapping("/courseUpdate")
	public String courseUpdate(int id , Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====teacherUpdate=======");

		/* 传入一个id */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("id", id);
		ACourse courses = this.usersService.findOneCourseService(condMap);
		model.addAttribute("courses", courses);
		return null ;
	}
	
	/**
	 * 更新提交页面
	 * @return
	 */
	@RequestMapping(value = "/courseUpdateSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String courseUpdateSubmit(int id , Model model ,HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("=====courseUpdateSubmit=======");
		/* 传入一个id */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("id", id);
		ACourse courses = this.usersService.findOneCourseService(condMap);
		model.addAttribute("courses", courses);
		
		/* 修改个人信息 */
		String name = request.getParameter("name");
		String score = request.getParameter("score");
		String content = request.getParameter("content");
		
		/* 设置值 */
		courses.setName(name);
		courses.setScore(Integer.valueOf(score));
		courses.setContent(content);
	
		/* 更新课程 */
		JSONObject resultJson = this.usersService.updateOneCourseService(courses);
		ConstatFinalUtil.LOGGER.info("数据库返回:{}",resultJson.toJSONString());
		/* 提示信息 */
		this.info = resultJson.getString(InfoEnum.VAR_INFO.getInfo());
	
		return resultJson.toJSONString();
	}
	
	
	/**
	 * 打开课程添加页面
	 * @return
	 */
	@RequestMapping("/coursesInsert")
	public String coursesInsert()
	{
		ConstatFinalUtil.LOGGER.info("=========coursesInsert=========");
		return null ;
	}
	
	/**
	 * 课程添加提交页面
	 * @return
	 */
	@RequestMapping(value = "/coursesInsertSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String coursesInsertSubmit(String name , String score , String content ,Model model)
	{
		ConstatFinalUtil.LOGGER.info("=========coursesInsertSubmit=========");
		
		ACourse course = new ACourse() ;
		
		/* 设置参数 */
		course.setName(name);
		course.setScore(Integer.valueOf(score));
		course.setContent(content);
		
		/* 调用service */
		JSONObject resultJson = this.usersService.saveOneCourseService(course);
		return resultJson.toJSONString() ;
	}
	
	/**
	 * 退出
	 * @return
	 */
	@RequestMapping("/loginOut")
	public String loginOut(HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("===teacher==loginOut=======");
		session.removeAttribute("lastLoginTime");
		session.removeAttribute("TeachersSess");
		return  "redirect:/login";
	}
}
